package simbot.example.listener.event.message.group.request.maniang;

import cn.hutool.core.util.ObjectUtil;
import love.forte.common.ioc.annotation.Depend;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.Filters;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.api.message.MessageContentBuilderFactory;
import love.forte.simbot.api.message.containers.GroupAccountInfo;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.Sender;
import love.forte.simbot.filter.MatchType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import simbot.example.data.domain.MnFans;
import simbot.example.data.domain.MnUser;
import simbot.example.data.service.MnFansService;
import simbot.example.data.service.MnUserService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: 祺诺
 * @Name; 谢钱枫 1053557904@qq.com
 * @Date: 2021/7/19 9:16
 */
@Service
public class GroupMnListen {
    /**
     * 通过依赖注入获取一个 "消息正文构建器工厂"。
     */
    @Depend
    private final MessageContentBuilderFactory messageContentBuilderFactory;
    @Autowired
    private MnUserService mnUserService;
    @Autowired
    private MnFansService mnFansService;

    @Autowired
    public GroupMnListen(MessageContentBuilderFactory messageContentBuilderFactory) {
        this.messageContentBuilderFactory = messageContentBuilderFactory;
    }

    @OnGroup
    @Filters(value = {
            @Filter(value = "马娘帮助", matchType = MatchType.EQUALS)
    })
    public void MnHelp(GroupMsg groupMsg, Sender sender) {
        String helpText = "角色注册: 角色注册\n" +
                "马娘注册:马娘注册 马娘姓名\n" +
                "查询全部马娘:马娘查询\n" +
                "马娘更新:马娘更新 ";
        sender.sendGroupMsg(groupMsg,helpText);
    }

    @OnGroup
    @Filters(value = {
            @Filter(value = "马娘注册", matchType = MatchType.STARTS_WITH)
    })
    public void MnReg(GroupMsg groupMsg
            , Sender sender) {
        try {
            String text = groupMsg.getText();
            String replace = text.replace("马娘注册", "");
            String mnName = replace.replace(" ", "");
            GroupAccountInfo accountInfo = groupMsg.getAccountInfo();
            MnUser mnUser = mnUserService.selectByCode(accountInfo.getAccountCode());
            if (ObjectUtil.isNull(mnUser)) {
                sender.sendGroupMsg(groupMsg, "您并未注册");
                return;
            }
            Boolean b = mnFansService.selectAllByUserIdAndName(mnUser.getId(), mnName);
            if (b == true) {
                //确认有此马娘
                sender.sendGroupMsg(groupMsg, "您已经注册过该马娘");
                return;
            }
            MnFans mnFans = new MnFans();
            mnFans.setUserId(mnUser.getId());
            mnFans.setName(mnName);
            mnFans.setCreateTime(LocalDateTime.now());
            mnFans.setUpdateTime(LocalDateTime.now());
            mnFans.setCreateNum(0L);
            mnFans.setUpdateNum(0L);
            boolean save = mnFansService.save(mnFans);
            if (save == false) {
                sender.sendGroupMsg(groupMsg, "保存该马娘出现错误,请联系管理员");
                return;
            }
            sender.sendGroupMsg(groupMsg, "注册马娘成功");
        } catch (Exception e) {
            sender.sendGroupMsg(groupMsg, "参数错误");
        }
    }

    @OnGroup
    @Filters(value = {
            @Filter(value = "角色注册", matchType = MatchType.EQUALS)
    })
    public void MnUserReg(GroupMsg groupMsg, Sender sender) {
        try {
            GroupAccountInfo accountInfo = groupMsg.getAccountInfo();
            String accountCode = accountInfo.getAccountCode();
            MnUser result = mnUserService.selectByCode(accountCode);
            if (ObjectUtil.isNotNull(result)) {
                sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "您已经注册过了~");
                return;
            }
            MnUser mnUser = new MnUser();
            mnUser.setCode(accountCode);
            boolean save = mnUserService.save(mnUser);
            if (save == false) {
                sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "保存出错~");
                return;
            }
            sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "\n注册成功,请记得添加和更新自己马娘的信息哦\n" +
                    "可以使用马娘帮助哦");
        } catch (Exception e) {
            sender.sendGroupMsg(groupMsg, "参数错误");
        }
    }

    @OnGroup
    @Filters(value = {
            @Filter(value = "马娘更新", matchType = MatchType.STARTS_WITH)
    })
    public void MnUpdate(GroupMsg groupMsg, Sender sender) {
        String text = groupMsg.getText();
        String mn = text.replace("马娘更新 ", "");
        String[] split = mn.split(" ");
        GroupAccountInfo accountInfo = groupMsg.getAccountInfo();
        String accountCode = accountInfo.getAccountCode();
        MnUser mnUser = mnUserService.selectByCode(accountCode);
        if (ObjectUtil.isNull(mnUser)) {
            sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "您没有注册~");
            return;

        }
        MnFans mnFans = mnFansService.findAllByUserIdAndName(mnUser.getId(), split[0]);
        if (ObjectUtil.isNull(mnFans)) {
            sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "您没有注册该马娘~");
            return;

        }
        try {
            mnFans.setCreateTime(mnFans.getUpdateTime());
            mnFans.setCreateNum(mnFans.getUpdateNum());
            mnFans.setUpdateTime(LocalDateTime.now());
            mnFans.setUpdateNum(Long.valueOf(split[1]));
        } catch (NumberFormatException e) {
            sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "您输入的数据格式有误~");
            return;
        }
        boolean b = mnFansService.updateById(mnFans);
        if (b == false) {
            sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "保存失败,请查看您保存的格式~");
            return;
        }
        sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "更新成功~");
    }

    @OnGroup
    @Filters(value = {
            @Filter(value = "马娘查询", matchType = MatchType.EQUALS)
    })
    public void mnselect(GroupMsg groupMsg, Sender sender) {

        GroupAccountInfo accountInfo = groupMsg.getAccountInfo();
        String accountCode = accountInfo.getAccountCode();
        MnUser mnUser = mnUserService.selectByCode(accountCode);
        if (ObjectUtil.isNull(mnUser)) {
            sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "您没有注册~");
            return;
        }
        List<MnFans> mnFans = mnFansService.selectByUserId(mnUser.getId());
        if (ObjectUtil.isNull(mnFans)) {
            sender.sendGroupMsg(groupMsg, "您没有马娘");
            return;
        }
        String s = new String();

        for (int i = 0; i < mnFans.size(); i++) {
            String result = mnFans.get(i).getName() +
                    ": 上次更新时间:" + mnFans.get(i).getCreateTime() +
                    " 上次更新时粉丝数:" + mnFans.get(i).getCreateNum() +
                    " 这次更新时间:" + mnFans.get(i).getUpdateTime() + " 这次更新时粉丝数:"
                    + mnFans.get(i).getUpdateNum() + "\n";
            s = s + result;
        }
        sender.sendGroupMsg(groupMsg, accountInfo.getAccountNickname() + "的马娘:\n" + s);
    }
}
